package com.demo2do.fms.admin.web.controller.supplier;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.SupplierProfileSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierInfoService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierEvent;
import com.demo2do.fms.entity.supplier.common.SupplierEventType;
import com.demo2do.fms.entity.supplier.common.SupplierInfo;
import com.demo2do.fms.entity.supplier.common.SupplierModifiedVersion;

/**
 * 公司信息管理
 * 
 * @author zhonghui
 * 
 */
@Controller
@RequestMapping("")
@MenuSetting("menu-supplier")
public class SupplierProfileController {
    
    public static final Logger logger = LoggerFactory.getLogger(SupplierProfileController.class);
    
    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private SupplierInfoService supplierInfoService;
    
    /**
     * list
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "/supplier-profile", method = RequestMethod.GET)
    @MenuSetting("submenu-self-supplier-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/supplier-profile-list");
        
        List<Supplier> suppliers = new ArrayList<Supplier>();
        
        if(login.isSuper()) {
            
            int totals = supplierService.count(Supplier.class);
            if(totals > 0){
                suppliers = supplierService.list(page.with(totals));
                modelAndView.addObject("page", page);
            }
            
        } else if (login.isSupplier()) {
            
            Supplier supplier = supplierService.load(Supplier.class, login.getSupplierId());
            suppliers.add(supplier);
            
        }
        
        modelAndView.addObject("suppliers", suppliers);
        return modelAndView;
        
    }
    
    /**
     *  search
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-self-supplier-management")
    @RequestMapping(value = "/supplier-profile", method = RequestMethod.GET, params= "filter")
    public ModelAndView onSearch(SupplierProfileSearch search , Page page, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/profile/supplier-profile-list");
        
        int totals = supplierService.count(Supplier.class , search.getFilter());
        if(totals > 0){
            
            List<Supplier> suppliers = supplierService.search(Supplier.class, search, page.with(totals));
            modelAndView.addObject("suppliers", suppliers);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
        }
        
        return modelAndView;
        
    }
    
    /**
     * change panel
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/suppliers/{supplierId}/change", method = RequestMethod.GET)
    @MenuSetting("submenu-self-supplier-management")
    public ModelAndView change(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/supplier-profile-change");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        SupplierInfo supplierInfo = supplierInfoService.load(supplierId);
        
        SupplierModifiedVersion version = supplierInfoService.prepareChange(supplier);
        modelAndView.addObject("version", version);
        
        modelAndView.addObject("supplier", supplier);
        modelAndView.addObject("supplierInfo", supplierInfo);
        
        return modelAndView;
        
    }
    
    /**
     * change
     * 
     * @param operator
     * @param supplierModifiedVersion
     * @return
     */
    @RequestMapping(value = "/suppliers/{supplierId}/change", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-self-supplier-management")
    public @ResponseBody HttpResult onChange(@Secure(property = "id") Long operator, 
                                             @Preloading("versionId") SupplierModifiedVersion supplierModifiedVersion) {

        HttpResult httpResult = new HttpResult();
        
        try {
            
            Supplier supplier = supplierService.load(Supplier.class, supplierModifiedVersion.getSupplierId());
            supplierInfoService.change(supplier, supplierModifiedVersion, operator);
            
            httpResult.success().message("变更申报成功");
            
        } catch (Exception e) {
            logger.error("#onChange() - change failed, exception={}", e);
            httpResult.fail("500").message("变更申报失败");
        }
        
        return httpResult;
        
    }
    
    /**
     * view reject reason
     * 
     * @param login
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/suppliers/{supplierId}/reject", method = RequestMethod.GET)
    @MenuSetting("submenu-self-supplier-management")
    public ModelAndView rejectReason(@Secure Login login , @PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/supplier-reject-reason");
        
        SupplierEvent supplierEvent = supplierService.getSupplierEvent(supplierId, SupplierEventType.INTRODUCE_REJECTED);
        modelAndView.addObject("supplierEvent", supplierEvent);
        
        return modelAndView;
        
    }
    
}
